package nl.ypmits.utils.as3.bitmap {
	import nl.ypmits.utils.as3.bitmap.input.BitmapModifierInput;

	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	/**
	 * @author Merten
	 */
	public class BitmapModTools {
		
		public static function getShapeFromBitmap( _i:BitmapModifierInput ):BitmapData {
			if (!_i.alpha.transparent) throw new Error("BitmapData alpha requires to have transparency");
			_i.source.copyPixels( _i.source, _i.destinationRectangle, _i.sourcePoint, _i.alpha, _i.alphaPoint, false);
			return _i.source;
		}
		
		public static function getRectFromBitmap( _i:BitmapModifierInput ):BitmapData {
			var bmp:BitmapData = new BitmapData(_i.destinationRectangle.width,_i.destinationRectangle.height, false, 0x000000 );
			bmp.copyPixels( _i.source, _i.destinationRectangle, _i.sourcePoint );
			return bmp;
		}
		
		public static function resample( _bmp:BitmapData, _perc:Number, _maintainBitmap:Boolean = false ):BitmapData {
			// Procentual factor Asuming 4:3 aspect ratio factor from source

			var m:Matrix = new Matrix();
			m.scale(_perc,_perc);
			
			var resampled:BitmapData = new BitmapData( _bmp.width*_perc, _bmp.height*_perc, false, 0x000000 );
			resampled.draw(_bmp, m, null, null, null, true);
			
			if (!_maintainBitmap) _bmp.dispose();

			return resampled;
		}
		
		public static function border( _bmp:BitmapData, _borderThickness:Number, _borderColor:Number = 0xEEEEEE ):BitmapData {
			_bmp.fillRect(new Rectangle( 0, 0, _bmp.width, _borderThickness ), _borderColor );
			_bmp.fillRect(new Rectangle( 0, 0, _borderThickness, _bmp.height ), _borderColor );
			_bmp.fillRect(new Rectangle( 0, _bmp.height-_borderThickness, _bmp.width, _borderThickness ), _borderColor );
			_bmp.fillRect(new Rectangle( _bmp.width-_borderThickness, 0, _borderThickness, _bmp.height ), _borderColor );
			return _bmp;	
		}
		
		public static function flipH( _b:BitmapData ):BitmapData {

			var m:Matrix = new Matrix();
			m.scale(-1,1);
			m.translate( _b.width, 0 );
			
			var bmp:BitmapData = new BitmapData(_b.width,_b.height, false, 0x000000 );
			bmp.draw( _b, m, null, null, null, true );
			
			return bmp;
		}
		
		public static function flipV():void {
			
		}
	}
}
